package org.groupg.poi.word;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import cn.hutool.core.io.resource.ResourceUtil;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;

public class POIWordReader {

    public static void main(String[] args) {
        // 示例文件路径
        String docFilePath = "001.doc";
        String docxFilePath = "001.docx";

        try {
            // 读取 .doc 文件
            FileInputStream fisDoc = new FileInputStream(ResourceUtil.getResource(docFilePath).getFile());
            HWPFDocument doc = new HWPFDocument(fisDoc);
            WordExtractor extractorDoc = new WordExtractor(doc);
            String docText = extractorDoc.getText();
            fisDoc.close();
            System.out.println(".doc 文件内容:");
            System.out.println(docText);

            // 读取 .docx 文件
            FileInputStream fisDocx = new FileInputStream(ResourceUtil.getResource(docxFilePath).getFile());
            XWPFDocument docx = new XWPFDocument(fisDocx);
            XWPFWordExtractor extractorDocx = new XWPFWordExtractor(docx);
            String docxText = extractorDocx.getText();
            fisDocx.close();
            System.out.println(".docx 文件内容:");
            System.out.println(docxText);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}